/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.lang.reflect.Type;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import modelo.Noticia;
import modelo.WebService;
import persistencia.NoticiaDAO;

/**
 *
 * @author Soares
 */
@Named(value="noticiaMB")

public class NoticiaMB {

    /**
     * Creates a new instance of NoticiaMB
     */
    
    private Noticia noticia;
    
    String secao;
    
    String ip;
    
    private NoticiaDAO dao;
    
    public NoticiaMB() throws SQLException,ParseException {
        
        dao = new NoticiaDAO();
        String data = "01/01/2014";
        SimpleDateFormat sf = new SimpleDateFormat("dd/mm/yyyy");
        Date d = (Date) sf.parse(data);   
        
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        
        String secao = request.getParameter("secao");
        
        secao = request.getParameter(secao);
       if (secao == null)
               {
                   ip = "lcalhost";
               }
       else{
           switch(secao){
               case "POL":
                   ip = "10.0.26.40";
                   break;
               case "CLA":
                   ip = "10.0.26.40";
                   break;
           }
       }       
    }
    
    public Noticia getNoticia(){
        
        return noticia;
    }
    
    public void setNoticia(Noticia noticia){
        this.noticia = noticia;
    }
    
    public List<Noticia> listar() throws SQLException, Exception{
       List<Noticia> lista; 
       WebService ws = new WebService();
       String retorno = ws.sendGet("http://"+ip+":8080/PortalNoticiaWS/webresources/portal/getNoticias");
       Gson g = new Gson();
       Type tipo = new TypeToken<ArrayList<Noticia>>(){}.getType();
       lista = g.fromJson(retorno,tipo);
       return lista;
    }
}
